﻿using System;
using System.Collections.Generic;
using System.Text;
using TomShane.Neoforce.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Warlock.Menu
{
    class OptionsWindow : Window
    {
        GraphicsDeviceManager graphics;
        ContentManager content;

        TabControl optionsOverview;
        TabPage Common;
        Label nameLabel;
        TextBox nameTextbox;
        Label resolutionLabel;
        ListBox resolutionsListbox;
        MenuItemButton applyButton;

        TabPage ControlsTabPage;

        /// <summary>
        /// Wird aufgerufen, nachdem die Aufloesung geaendert wurde
        /// </summary>
        public event ResolutionChangedEventHandler ResolutionChanged;

        public OptionsWindow(Manager manager, GraphicsDeviceManager graphics, ContentManager content)
            : base(manager)
        {
            this.graphics = graphics;
            this.content = content;
        }
        public override void Init()
        {
            base.Init();

            Manager manager = Game1.Manager;

            this.Width = 300;
            this.Height = 400;

            applyButton = new MenuItemButton();
            applyButton.Init(manager, content);
            applyButton.Position = new Vector2(150, 100);
            applyButton.Button.Click += new TomShane.Neoforce.Controls.EventHandler(Button_Click);
            applyButton.Button.Visible = true;
            applyButton.Button.Parent = this;
            applyButton.Button.Text = "Apply";
            applyButton.Button.Width = 70;
            applyButton.Button.Height = 40;

            nameLabel = new Label(manager);
            nameLabel.Init();
            nameLabel.Left = 30;
            nameLabel.Top = 20;
            nameLabel.Text = "Name:";
            nameLabel.Visible = true;
            nameLabel.Parent = this;

            nameTextbox = new TextBox(manager);
            nameTextbox.Init();
            nameTextbox.Left = 30;
            nameTextbox.Top = 40;
            nameTextbox.Text = Properties.Settings.Default.PlayerName;
            nameTextbox.Parent = this;

            resolutionLabel = new Label(manager);
            resolutionLabel.Init();
            resolutionLabel.Left = 30;
            resolutionLabel.Top = 80;
            resolutionLabel.Parent = this;
            resolutionLabel.Text = "Aufloesung:";

            resolutionsListbox = new ListBox(manager);
            resolutionsListbox.Init();
            resolutionsListbox.Left = 30;
            resolutionsListbox.Top = 110;
            resolutionsListbox.Width = 100;
            resolutionsListbox.Items.Add("800*800");
            resolutionsListbox.Items.Add("1024*768");
            resolutionsListbox.Parent = this;
        }

        void Button_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            if (resolutionsListbox.ItemIndex >= 0)
            {
                string res = resolutionsListbox.Items[resolutionsListbox.ItemIndex].ToString();
                Size size = ParseResolution(res);
                graphics.PreferredBackBufferWidth = size.Width;
                graphics.PreferredBackBufferHeight = size.Height;
                graphics.ApplyChanges();
                OnResolutionChanged();
            }
            SaveSettings();
        }
        void OnResolutionChanged()
        {
            if (ResolutionChanged != null)
            {
                ResolutionChanged(new ResolutionChangedEventArgs(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight));
            }
        }
        Size ParseResolution(string res)
        {
            //Stern am Ende hinzufuegen um den string besser splitten zu koennen
            res += '*';
            string sWidth = "";
            string sHeight = "";
            int count = 0;
            while (char.IsNumber(res[count]))
            {
                sWidth += res[count];
                count++;
            }
            //Den Stern ueberspringen
            count++;
            while (char.IsNumber(res[count]))
            {
                sHeight += res[count];
                count++;
            }
            return new Size(int.Parse(sWidth), int.Parse(sHeight));
        }

        public void SaveSettings()
        {
            Properties.Settings.Default.PlayerName = nameTextbox.Text;
            Properties.Settings.Default.Save();
        }
    }
}